/**
 * 2017年4月30日
 */
package cn.edu.bjtu.entity.impl;

import java.util.concurrent.locks.ReadWriteLock;

import cn.edu.bjtu.entity.ExecuteContext;
import cn.edu.bjtu.entity.ViewRenderExecuteResult;

/**
 * 这个关于HTTPCLIENT线程安全问题还没完全弄明白
 * 弄明白了之后在改这个类.
 * 这个 方法是同步执行的.会阻塞tomcat http处理请求
 * @author Alex
 *
 */
public abstract class BaseLocalContext<R> implements ExecuteContext,ViewRenderExecuteResult<R>{	
	protected boolean executing = false;
	protected boolean async = false;
	protected boolean failed = false;
	//本机执行的任务可能需要访问http
	
	protected ReadWriteLock lock; 
	protected String msg = null;
	
	String inputDir = null;
	String outputDir = null;
	
	public BaseLocalContext(ReadWriteLock lock,String inputDir,String outputDir) {
		this.lock = lock;
		this.inputDir = inputDir;
		this.outputDir = outputDir;
	}
	protected abstract void fireInternal() throws Exception;
	public void fire() {
		try{
			
			executing = true;
			fireInternal();
			failed = false;
		}catch(Exception e){
			failed = true;
			msg = e.getMessage();
		}finally{
			executing =false;
		}
		
	}
	
	@Override
	public boolean isNoCache() {
		return true;
	}

	@Override
	public String getInputDir() {
		return inputDir;
	}

	@Override
	public String getOutPutDir() {
		return outputDir;
	}

	@Override
	public boolean isAsync() {
		return async;
	}
	
	@Override
	public boolean isExist() {
		return true;
	}

	@Override
	public boolean isSuccess() {
		// TODO Auto-generated method stub
		return !failed;
	}

	@Override
	public boolean isFailed() {
		// TODO Auto-generated method stub
		return failed;
	}

	@Override
	public String getTipMessage() {
		// TODO Auto-generated method stub
		return msg;
	}
	
	@Override
	public boolean isExecuting() {
		// TODO Auto-generated method stub
		return executing;
	}
}
